#!/bin/bash

# 定义变量
APP_NAME="blog-admin"
JAR_FILE="$APP_NAME.jar"
SERVER_IP="47.102.216.101"
SERVER_USERNAME="root"
REMOTE_DIR="/andy/admin"
LOCAL_JAR_PATH="blog-admin/target/$JAR_FILE"
IDENTITY_FILE="private.pem"

# 构建并打包应用
echo "Building Spring Boot application..."
mvn clean package -DskipTests

if [ $? -eq 0 ]; then
    echo "Build successful."
else
    echo "Build failed. Exiting..."
    exit 1
fi

# 上传到服务器
echo "Uploading $JAR_FILE to server..."
scp -i $IDENTITY_FILE $LOCAL_JAR_PATH $SERVER_USERNAME@$SERVER_IP:$REMOTE_DIR

if [ $? -eq 0 ]; then
    echo "Upload successful."
else
    echo "Upload failed. Exiting..."
    exit 1
fi


# （可选）在服务器上执行部署命令
# 注意：根据实际情况修改命令，比如使用systemd、supervisor等服务管理器
# 可以传递给远程脚本的参数（如果需要）
ARGS="arg1"

# 通过SSH执行远程脚本
ssh -i $IDENTITY_FILE ${SERVER_USERNAME}@${SERVER_IP} "bash ${REMOTE_DIR}/remote_shell.sh ${ARGS}"

echo "Deployment completed."
